package com.wesley.fritesfinder.parser;

import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.net.URL;
import java.util.ArrayList;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;

import com.wesley.fritesfinder.model.FoodShop;

public class XMLParser {

	
	private XMLReader initReader() throws SAXException, ParserConfigurationException {
		SAXParserFactory factory = SAXParserFactory.newInstance();
		
		SAXParser parser = factory.newSAXParser();
		XMLReader reader = parser.getXMLReader();
		
		return reader;
		
	}
	
	
	public ArrayList<FoodShop> parseFoodshops(String xml) {
		try {
			XMLReader xmlReader = initReader();
			FritesHandler fritesHandler = new FritesHandler();
			xmlReader.setContentHandler(fritesHandler);
			xmlReader.parse(new InputSource());
			return fritesHandler.retrieveFoodshopList();
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		} catch (SAXException e) {
			e.printStackTrace();
			return null;
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
			return null;
		}
	}
	
}